<?php
namespace api\service;

use api\service\interfaces\IBrandService;
use herosphp\bean\Beans;
use herosphp\core\Loader;
use herosphp\utils\AjaxResult;
use herosphp\utils\ArrayUtils;
use herosphp\core\Log;

Loader::import('api.service.interfaces.IBrandService');

/**
 * 牌号 api(Service)接口实现
 * @author yuliang
 */
class BrandService implements IBrandService
{


    /**
     * 获取类型
     * @return AjaxResult
     */
    public function getTypes()
    {
        $service = Beans::get('product.productType.service');

        $items = $service->field('id,name')->find();

        return $items ? new AjaxResult(AjaxResult::OP_SUCCESS, "获取数据成功", $items) : new AjaxResult(AjaxResult::OP_FAILURE, "获取数据失败", null);

    }

    /**
     * 获取品名
     * @param $typeId 类型id
     * @param $name 品名关键词搜索
     * @return AjaxResult
     */
    public function getProducts($typeId, $name = '')
    {

        $service = Beans::get('product.brand.service');

        if ($name == '') {

            $conditions = array('typeid' => $typeId);
        } else {

            $conditions = array('typeid' => $typeId, 'name' => "%$name%");
        }

        $items = $service->getItems($conditions, 'id,name,typeid As type_id');

        return $items ? new AjaxResult(AjaxResult::OP_SUCCESS, "获取数据成功", $items) : new AjaxResult(AjaxResult::OP_FAILURE, "获取数据失败", null);

    }

    /**
     * 获取厂家
     * @param $typeId  类型id
     * @param $productId 产品id
     * @param $name 厂家关键词搜索
     * @return AjaxResult
     */
    public function getFactorys($typeId, $productId, $name = '')
    {

        $gradeModel = Loader::model("Grade");

        $sql = "Select id,name From factory Where id In(Select distinct facid As id from grade Where typeid='$typeId' And brandid='$productId')";

        $items = $gradeModel->getDB()->excute($sql)->fetchAll($fetch_style = 2);

        return $items ? new AjaxResult(AjaxResult::OP_SUCCESS, "获取数据成功", $items) : new AjaxResult(AjaxResult::OP_FAILURE, "获取数据失败", null);

    }

    /**
     * 获取牌号
     * @param $typeId 类型id
     * @param $productId 产品id
     * @param $factoryId 厂家id
     * @return AjaxResult
     */
    public function getBrands($typeId, $productId, $factoryId)
    {

        $service = Beans::get('product.grade.service');

        $items = $service->getItems(array('typeid' => $typeId, 'brandid' => $productId, 'facid' => $factoryId),
            'id,name,`typeid` As `type_id`,`brandid` As `product_id`,`facid` As `factory_id`');

        return $items ? new AjaxResult(AjaxResult::OP_SUCCESS, "获取数据成功", $items) : new AjaxResult(AjaxResult::OP_FAILURE, "获取数据失败", null);

    }


    /**
     * 根据提交人来获取牌号数据
     * @param $pagesize
     * @param $page
     * @param $order 排序
     * @param $search_arr
     * @return AjaxResult
     */
    public function getBrandsByMemberId($pagesize, $page, $order, $search_arr)
    {

        $service = Beans::get('product.grade.service');

        if ($search_arr['start_time'] !== '' && $search_arr['start_time'] !== NULL) {
            $conditions['addtime'] = array();
            $conditions['addtime']['>'] = $search_arr['start_time'] . ' 00:00:00';
        }
        if ($search_arr['end_time'] !== '' && $search_arr['end_time'] !== NULL) {
            if (!$conditions['addtime']) {
                $conditions['addtime'] = array();
            }
            $conditions['addtime']['<='] = $search_arr['end_time'] . ' 23:59:59';
        }
        if ($search_arr['saas_user_id'] != '') {

            $conditions['saas_user_id'] = $search_arr['saas_user_id'];
        }

        $result['items'] = $service->getItems($conditions, '*', $order, array($page, $pagesize));

        $result['total'] = $service->count($search_arr);

        return $result ? new AjaxResult(AjaxResult::OP_SUCCESS, "获取数据成功", $result) : new AjaxResult(AjaxResult::OP_FAILURE, "获取数据失败", null);


    }

    /**
     * 添加牌号
     * @param $name
     * @param $typeid
     * @param $brandid
     * @param $facid
     * @param $saas_user_id
     * @param $from
     */
    public function addBrand($name, $typeid, $brandid, $facid, $saas_user_id, $from)
    {

        $gradeModel = Loader::model('Grade');

        $data['name'] = $name;
        $data['typeid'] = $typeid;
        $data['brandid'] = $brandid;
        $data['facid'] = $facid;

        $item = $gradeModel->getItem($data);

        if ($item) {

            return new AjaxResult(AjaxResult::OP_SUCCESS, "牌号已添加，不可重发添加", array('id' => $item['id']));

        } else {

            $data['addtime'] = date('Y-m-d H:i:s');
            $data['updatetime'] = date('Y-m-d H:i:s');

            $data['saas_user_id'] = $saas_user_id;

            $data['ischecked'] = 0;
            $data['from'] = $from;

            $last_id = $gradeModel->add($data);

            return $last_id ? new AjaxResult(AjaxResult::OP_SUCCESS, "牌号添加成功", array('id' => $last_id)) : new AjaxResult(AjaxResult::OP_FAILURE, "获取数据失败", null);

        }


    }

    /**
     * 牌号搜索
     * @param $brand_name
     * @return AjaxResult
     */
    public function searchBrand($brand_name, $brand_name2, $saas_user_id)
    {

        $gradeService = Beans::get('product.grade.service');

        if ($brand_name2 == '') {

            $conditions = array('name' => "%$brand_name%", 'ischecked' => 1);
        } else {
            $conditions = array('name' => "$brand_name", '|name' => "%$brand_name2%", 'ischecked' => 1);

        }
        //公告牌号库
        $allItems['public'] = $gradeService->getItems($conditions, 'id,name,typeid,brandid,facid', null, 50);

        //自定义牌号库
        $allItems['custom'] = array();

        if ($saas_user_id != '') {

            $conditions = array('saas_user_id' => $saas_user_id);

            $allItems['custom'] = $gradeService->getItems($conditions, 'id,name,typeid,brandid,facid', null, 8);
        }

        return $allItems ? new AjaxResult(AjaxResult::OP_SUCCESS, "获取数据成功", $allItems) : new AjaxResult(AjaxResult::OP_FAILURE, "获取数据失败", null);

    }

    /**
     * 根据关键词判断是否时品名
     * @param $k
     * @return bool
     */
    public function searchProductByKeyword($keyword)
    {

        $brandModel = Loader::model('Brand');

        //查询品名或牌号数据
        $sql = "Select g.id from grade As g Left Join brand As b On g.brandid=b.id Where b.name ='$keyword'";

        $items = $brandModel->query($sql);

        return $items[0] ? true : false;

    }

    /**
     * 根据关键词判断是否时牌号
     * @param $k
     * @return bool
     */
    public function searchBrandByKeyword($keyword)
    {

        $gradeModel = Loader::model('Grade');

        //查询品名或牌号数据
        $sql = "Select g.id from grade As g Left Join brand As b On g.brandid=b.id Where g.name='$keyword'";

        $items = $gradeModel->query($sql);

        return $items[0] ? true : false;

    }

    public function searchBrandByPb($product, $brand)
    {

        $gradeModel = Loader::model('Grade');

        //查询品名或牌号数据
        $sql = "Select g.id from grade As g Left Join brand As b On g.brandid=b.id Where g.name='$brand' And b.name='$product'";

        $items = $gradeModel->query($sql);

        return $items[0] ? true : false;

    }


    /**
     * 根据两个关键词来查询品名和牌号数据
     * @param $k1
     * @param $k2
     */
    public function searchBrandByTwoKeyword($k1, $k2)
    {
        $result = array('product' => '', 'brand' => '');

        if ($k1 != '' && $k2 != '') {

            if ($this->searchBrandByPb($k1, $k2)) {
                $result['product'] = $k1;
                $result['brand'] = $k2;
            }

        } elseif ($k1 != '' && $k2 == '') {

            if ($this->searchProductByKeyword($k1)) {

                $result['product'] = $k1;


            } elseif ($this->searchBrandByKeyword($k1)) {

                $result['brand'] = $k1;

            }

        } elseif ($k1 == '' && $k2 != '') {

            if ($this->searchProductByKeyword($k2)) {

                $result['product'] = $k2;


            } elseif ($this->searchBrandByKeyword($k2)) {

                $result['brand'] = $k2;

            }

        }

        return new AjaxResult(AjaxResult::OP_SUCCESS, "获取数据成功", $result);
    }


}
